package com.qk.management.controller;


import com.qk.common.Result;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.dto.customer.CustomerDTO;
import com.qk.dto.customer.CustomerPageDTO;
import com.qk.entity.Customer;
import com.qk.management.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/customers")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @PutMapping
    public Result updateCustomer(@RequestBody Customer customer){
        customerService.updateCustomer(customer);
        return  Result.success();
    }


    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id){
        if (Objects.isNull(id))
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        return Result.success(customerService.getById(id));
    }

    @PostMapping
    public Result save(@RequestBody CustomerDTO dto){
        customerService.addCoustomer(dto);
        return Result.success();
    }

    @GetMapping
    public Result getCustomerPageList(CustomerPageDTO dto){
        return Result.success(customerService.getCustomerPageList(dto));
    }

}
